package com.twinkle.controller.classifier;

import com.twinkle.model.ClassifierResult;
import com.twinkle.service.ClassifierResultService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @title: ClassifierResultController
 * @description: 分类算法接口类
 * @author: Paul
 * @date: 2023/3/23 13:34
 */
@Controller
@RequestMapping("/algo/classifierresult")
public class ClassifierResultController {

    @Autowired
    ClassifierResultService classifierResultService;

    @RequestMapping(value = "/resultpage", method = { RequestMethod.GET })
    public String resultPage(HttpServletRequest request) {
        return "algo_plat/classifier/resultgrid";
    }

    @ResponseBody
    @RequestMapping(value="/resultlist" , method={RequestMethod.POST})
    public String resultList(HttpServletRequest request){
        String page = request.getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数
        String rows = request.getParameter("rows"); // 取得每页显示行数，,注意这是jqgrid自身的参数
        return classifierResultService.getAllClassifierResultJson(Integer.parseInt(page) , Integer.parseInt(rows));
    }

    @RequestMapping(value="/resultdetail" , method={RequestMethod.GET})
    public String getResultDetail(Model model, @Param("id") String id){
        ClassifierResult classifierResult = classifierResultService.getClassifierResultById(id);
        model.addAttribute("classifierResult", classifierResult);
        if (classifierResult.getAlgoName().equals("decision_tree")) {
            model.addAttribute("algoName", "决策树");
        }else if (classifierResult.getAlgoName().equals("logistic_regression")) {
            model.addAttribute("algoName", "逻辑回归");
        }else if (classifierResult.getAlgoName().equals("random_forest")) {
            model.addAttribute("algoName", "随机森林");
        }else if (classifierResult.getAlgoName().equals("svc")) {
            model.addAttribute("algoName", "SVC");
        } else if (classifierResult.getAlgoName().equals("naive_bayes")) {
            model.addAttribute("algoName", "朴素贝叶斯");
        }
        return "algo_plat/classifier/classifier_result";
    }

    @ResponseBody
    @RequestMapping(value="/search" , method={RequestMethod.POST})
    public String search(HttpServletRequest request, @Param("algoName") String algoName, @Param("appName") String appName){
        String page = request.getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数
        String rows = request.getParameter("rows"); // 取得每页显示行数，,注意这是jqgrid自身的参数
        System.out.println("page : " + page + ";rows : " + rows);
        return classifierResultService.getAllClassifierResultJsonBySearch(Integer.parseInt(page) , Integer.parseInt(rows), algoName, appName);
    }

}
